var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatar: "",
        gender: ['男', '女', ],
        genderI: 0, //性别索引
        issuingDate: "", //发行日期
        dLID: "", //发行日期
        driversZm: "", //驾驶证正面图片
        driversFm: "", //驾驶证反面图片
        iLN: "", //驾驶证号码
        mobile: "", //手机号码
        lookD: true, //是否隐藏驾驶证号码
        save: false,
        focus: false,
        disable: false,
        num: 60,
        validDate: "", //有效日期
        deadline: "", //截止日期
        driversT: ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", ],
        driversI: 0, //驾驶证类型索引
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.item) {
            var itemDetail = JSON.parse(options.item)
            this.setData({
                itemDetail: itemDetail,
                id: itemDetail.id,
                genderI: itemDetail.sex - 1,
                driverLicenseNumberY: itemDetail.driverLicenseNumber.substr(0, 4) + '*********' + itemDetail.driverLicenseNumber.substr(13),
                driverLicenseNumber: itemDetail.driverLicenseNumber,
                driversZm: itemDetail.driverLicense,
                driversFm: itemDetail.driverLicenseSecondaryPage,
                validDate: itemDetail.driveLicenseExpiryBeginDate,
                deadline: itemDetail.driveLicenseExpiryEndDate,
                issuingDate: itemDetail.driveLicenseIssuingDate,
                driversI: itemDetail.driverLicenseType - 1,
                distributeAddr: itemDetail.distributeAddr
            })
        }

    },
    lookD(e) {
        this.setData({
            lookD: !this.data.lookD
        })
    },
    driveLicenseExpiryBeginDate(e) {
      console.log(e)
        this.setData({
                validDate: e.detail.value
            })
            // dLID: e.detail.value
    },
    deadline(e){
      this.setData({
        deadline: e.detail.value
      })
      
    },
    bindgenderChange(e) {
        this.setData({
            issuingDate: e.detail.value
        })
    },
    driverLicenseType(e) {
        this.setData({
            driversI: e.detail.value
        })
    },
    driversZ() {
        this.upLoadImage(3)
            // console.log(datas)
    },
    driversF() {
        this.upLoadImage(4)
            // console.log(datas)
    },
    driverLicenseNumber(e) {
        this.setData({
            driverLicenseNumber: e.detail.value
        })
    },
    identityCardNumber(e) {
        this.setData({
            iLN: e.detail.value
        })
    },
    emergencyContactTel(e) {
        this.setData({
            eCT: e.detail.value
        })
    },
    tel(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    formSubmit(e) {
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        var vlu = e.detail.value;
        // console.log(this.isCardID(vlu.identityCardNumber), "ssssssssssss")
        console.log(app.getApi)
        if (vlu.driverLicenseNumber == "") {
            this.showMoadl("驾驶证号不能为空")
            return;
        }
        if (vlu.distributeAddr == "") {
            this.showMoadl("地址不能为空")
            return;
        }
        if (vlu.driveLicenseIssuingDate == "") {
            this.showMoadl("初次领证日期不能为空")
            return;
        }
        if (vlu.identityCardNumberBeginDate == "") {
            this.showMoadl("驾驶证有效日期不能为空")
            return;
        }
        if (vlu.identityCardNumberEndDate == "") {
            this.showMoadl("驾驶证到期日期不能为空")
            return;
        }
        var datas = vlu
        datas.driverLicenseType = parseInt(vlu.driverLicenseType) + 1
        datas.driverLicense = this.data.driversZm
        datas.driverLicenseSecondaryPage = this.data.driversFm
        datas.id = app.getApi.drivers.id

        app.getApi.request("/api/Drivers/LicenseCertification", datas, "POST", (res) => {
            if (res.data.responseCode == 1000) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                });
                app.getApi.drivers.driveLicenseCertificationStatus = 2
                setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000)
            } else {
                wx.showToast({
                    title: '保存失败',
                    icon: 'loading',
                    duration: 2000
                });
            }
        }, (res) => {})


    },
    showMoadl(title) {
        wx.showModal({
            title: '提示',
            content: title,
            success: function(res) {
                if (res.confirm) {

                }
            }
        })
    },
    upLoadImage(isType) {
        var that = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths

                wx.uploadFile({
                    url: 'https://driverAPI.dwilms.com/api/Upload/UploadOnePic', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {},
                    success: function(res) {
                        var data = JSON.parse(res.data)
                        console.log(data)
                        switch (isType) {
                            case 1:
                                that.setData({
                                    identityZm: data.obj
                                })
                                break;
                            case 2:
                                that.setData({
                                    identityFm: data.obj

                                })
                                break;
                            case 3:
                                that.setData({
                                    driversZm: data.obj

                                })
                                break;
                            case 4:
                                that.setData({
                                    driversFm: data.obj

                                })
                                break;
                            case 5:
                                app.getApi.request("/api/Drivers/UpdateAvatar", { driverId: app.getApi.drivers.id, avatarUrl: data.obj }, "POST", (res) => {
                                    that.setData({
                                        avatar: data.obj

                                    })
                                }, (res) => {});
                        }


                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    changePhone(e) { //获取号码
        let str = e.detail.value;
        this.setData({
            phoneNum: str
        })
    },
    getCode: function() { //发送验证码
        if (this.data.disable) return;
        let phone = this.data.phoneNum,
            _this = this,
            code = "123456",
            flag = /^1[34578]\d{9}$/.test(phone);
        if (!flag) {
            wx.showModal({
                title: '提示',
                content: '请输入正确的手机号码',
                showCancel: false
            })
            return;
        }
        this.setData({
            disable: true,
            regCode: code
        })
        let timer = setInterval(function() {
            let num = _this.data.num;
            _this.setData({
                num: --num
            })
            if (_this.data.num == 1) {
                clearInterval(timer);
                _this.setData({
                    num: 60,
                    disable: false
                })
            }
        }, 1000)
        wx.getProducts("xntalk.login", "POST", {})
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})